<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\CustomerListParams;
use App\Services\Customer\CustomerService;
use Illuminate\Http\Request;
/**
 * 客户管理控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class CustomerController extends BaseController
{
    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 客户列表
     *
     * @throws
     * @return
     */
    public function getList()
    {
        $param = CustomerListParams::create();
        $ret = CustomerService::getInstance()->getList($param);
        return $this->success($ret);
    }

    /**
     * 更新客户信息
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function update(Request $request)
    {
        $customerId = $request->input('customer_id', 0);
        $data = [
            'grade_id' => $request->input('grade_id', 1),
            'group_id' => $request->input('group_id', 1)
        ];
        $ret = CustomerService::getInstance()->update($customerId, $data);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::CUSTOMER_UPDATE_FAIL);
    }

    /**
     * 删除客户
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function delete(Request $request)
    {
        $customerId = $request->input('customer_id', 0);
        $ret = CustomerService::getInstance()->delete($customerId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::CUSTOMER_DELETE_FAIL);
    }
}
